from contextlib import asynccontextmanager

from fastapi import FastAPI

from api.short import router_short
from api.user import router_uesr


@asynccontextmanager
async def lifespan(app: FastAPI):
    _ = app
    from db.database import async_engine, Base
    async def init_create_table():
        async with async_engine.begin() as conn:
            # await conn.run_sync(Base.metadata.drop_all)
            await conn.run_sync(Base.metadata.create_all)

    await init_create_table()
    yield


app = FastAPI(title='fastapi集成短链实战案例', openapi_url="/api/v1/openapi.json", lifespan=lifespan)

app.include_router(router_short)
app.include_router(router_uesr)

print('启动！')
if __name__ == '__main__':
    import uvicorn

    uvicorn.run(app='app:app', host="127.0.0.1")
